package com.scu.hotelsystem.controller;

import com.scu.hotelsystem.pojo.VipCate;
import com.scu.hotelsystem.service.VipCateService;
import com.scu.hotelsystem.util.PageInfo;
import com.scu.hotelsystem.util.Result;
import com.scu.hotelsystem.util.search.VipCateSearch;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
public class VipCateController {
    @Autowired
    private VipCateService vipCateService;

    @RequestMapping("/vipCateListPage")
    public String vipCateListPage(){
        return "vipcatelist";
    }

    @RequestMapping("/searchVipCateListRest")
    @ResponseBody
    public Result searchVipCateListRest(VipCateSearch vipCateSearch, PageInfo pageInfo){
        List<VipCate> vipCateList;
        VipCate search;
        int count;

        search = new VipCate(vipCateSearch);
        vipCateList = vipCateService.searchVipCateList(search, pageInfo.index(), pageInfo.limit());
        count = vipCateList.size();

        return new Result(count, vipCateList);
    }

    @RequestMapping("/vipCateEditPage")
    public ModelAndView vipCateEditPage(VipCateSearch vipCateSearch){
        List<VipCate> vipCateList;
        ModelAndView view;

        view = new ModelAndView("vipcateedit");
        VipCate search = new VipCate(vipCateSearch);
        vipCateList = vipCateService.searchVipCateList(search, 0, 1);
        if (vipCateList.size() != 0){
            view.addObject("vipCate", vipCateList.get(0));
        }

        return view;
    }

    @RequestMapping("/updateVipCate")
    @ResponseBody
    public Result updateVipCate(VipCate vipCate){
        int n;
        n = vipCateService.updateVipCate(vipCate);

        if (n == 1){
            return new Result();
        }else {
            return new Result(1, "未知错误");
        }
    }

    @RequestMapping("/vipCateAddPage")
    public String vipCateAddPage(){
        return "vipcateadd";
    }

    @RequestMapping("/addVipCate")
    @ResponseBody
    public Result addVipCate(VipCate vipCate){
        int n;
        n = vipCateService.addVipCate(vipCate);

        if (n == 1 || n == 2){
            return new Result();
        }else {
            return new Result(1, "未知错误");
        }
    }

    @RequestMapping("/deleteVipCate")
    @ResponseBody
    public Result deleteVipCate(int[] idList){
        if (idList.length == 0){
            return new Result(2, "列表为空");
        }

        int n;
        try{
            n = vipCateService.deleteVipCate(idList);
        }catch (IllegalArgumentException e){
            n = 0;
        }

        if (n == 0){
            return new Result(1, "失败");
        }else {
            return new Result();
        }
    }
}
